package com.app.sysmodule.httpcommon.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.app.sysmodule.exception.ServiceException;
import com.app.sysmodule.httpcommon.service.HttpCommonService;
import com.app.sysmodule.utils.HttpRequestUtil;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * Created by song on 2017/11/8.
 */
public class HttpCommonServiceImpl implements HttpCommonService {
    private static Logger log = Logger.getLogger(HttpCommonServiceImpl.class);
    public JSONObject handleMap(String url, String methodName, Map<String, Object> parm, Map<String, String> headers) throws ServiceException {
        JSONObject  jsonObject = new JSONObject();
        try{
            HttpRequestUtil httpRequestUtil = new HttpRequestUtil();
            log.info(methodName+"请求数据url:"+url+"------parm"+parm);
            jsonObject = httpRequestUtil.reqPost(url,parm,headers);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException();
        }
        return jsonObject;
    }

    public JSONObject handleJson(String url, String methodName, JSONObject jsonObject, Map<String, String> headers) throws ServiceException {
        JSONObject  json = new JSONObject();
        try{
            HttpRequestUtil httpRequestUtil = new HttpRequestUtil();
            log.info(methodName+"请求数据url:"+url+"------jsonObject"+jsonObject);
            if(null != jsonObject){
                json = httpRequestUtil.reqPost(url,jsonObject.toString(),headers);
            }else {
                json = httpRequestUtil.reqPost(url,"",headers);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException();
        }
        return json;
    }
}
